﻿using MainService.ZNQD;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using MainService.UISERVER;
using MainService;
namespace StartMain
{
    public partial class AppSystemFrm : Form
    {
        public Solution solution { get;set; }
        public AppSystemFrm(Solution solution)
        {
            InitializeComponent();
            this.solution = solution;
        }

        private void BtnCannel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            LoadFrm loader = new LoadFrm();
            SendOrPostCallback spc = new SendOrPostCallback(SaveAppSystem);
            loader.RegisterMesthod(spc,loader);
            loader.ShowDialog();
        }

        private void SaveAppSystem(object state)
        {
            LoadFrm loader = state as LoadFrm;
            try
            {
                loader.SetValueMess(10, "正在创建对象!");
                Systems systems = new Systems();
                AppSystem system = new AppSystem();
                systems.Childs.Add(system);
                system.appSystemName = txtAppSysName.Text.Trim();
                system.appSystemDescription = txtSystemDescription.Text.Trim();
                system.solutionName = solution.SolutionName;
                system.solutionChinaName = solution.SolutionChinaName;
                system.solutionVersion = solution.Version;
                system.solutionId = solution.SolutionID;
                system.userId = MyServer.UserID;
                system.appSysteCreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                system.solutionDescription = solution.Description;
                loader.SetValueMess(20, "正在保存到服务器!");
                MyResult result = UIServer.SaveAppSystem(systems);
                if (result.Code == "success")
                {
                    system.id = result.Keys[0];
                    MyServer.CurrentAppSystem = system;
                    MyServer.CurrentSolution = solution;
                    MyServer.EditModel = EditModel.AppSystem;
                    MyServer.EditType = EditType.Create;
                    loader.SetValueMess(100, "应用系统创建成功!");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(result.Msg);
                    MyServer.Log(result.Msg);
                }
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
                MyServer.Log(Err.ToString());
            }
            finally 
            {
                loader.Close();
                this.Close();
            }
        }
    }
}
